<?php
// +----------------------------------------------------------------------
// | snake
// +----------------------------------------------------------------------
// | Copyright (c) 2016~2022 http://baiyf.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: NickBai <1902822973@qq.com>
// +----------------------------------------------------------------------
namespace app\model;

use think\Model;

class NodeModel extends Model
{
    // 确定链接表名
    protected $name = 'node';


    /**
     * 获取节点数据
     * @return mixed
     */
    public function getNodeList($where)
    {
        return $this->where($where)->order("sort desc")->select();
//        return $this->where($where)->order("sort desc")->column('');
    }

    /**
     * 插入节点信息
     * @param $param
     */
    public function insertNode($param)
    {
        try{
            $this->save($param);
            return msg(1, url('node/index'), '添加节点成功');
        }catch(PDOException $e){
            return msg(-2, '', $e->getMessage());
        }
    }

    /**
     * 编辑节点
     */
    public function editNode($param)
    {
        try{

            $this->save($param, ['id' => $param['id']]);
            return msg(1, url('node/index'), '编辑节点成功');
        }catch(PDOException $e){

            return msg(-2, '', $e->getMessage());
        }
    }

    /**
     * 删除节点
     * @param $id
     */
    public function delNode($id)
    {
        try{
            $this->where('id', $id)->delete();
            return msg(1, '', '删除节点成功');

        }catch(PDOException $e){
            return msg(-1, '', $e->getMessage());
        }
    }

    /**
     * 根据节点id获取子节点是否存在
    */
    public function getChild($id)
    {
       return $this->where("pid",$id)->select();
    }

    /**
     * 查询一条节点信息
    */
    public function getNodeFind($id)
    {
        return $this->where("id",$id)->find();
    }

}
